home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Acere (PowerPlant, Game) 1.2 / AcereÄ.sit / Acereƒ / Code / CTextDoc.cp < prev    next >
Text File  |  1995-03-05  |  7KB  |  297 lines

  1. // ===========================================================================
  2. //    CTextDoc.cp                        ⌐1994 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //
  5. //    A simple text document class. It can handle opening, saving, and
  6. //    reverting TEXT files. It displays the text in a Window and can
  7. //    print the text.
  8.  
  9. #include "AcereApp.h"
  10. #include "CTextDoc.h"
  11. #include "WellStack.h"
  12. #include "WellFreeCell.h"
  13. #include "WellDeck.h"
  14. #include "CardDeck.h"
  15.  
  16. #include "CardWindow.h"
  17. #include <LFile.h>
  18. #include <LPrintout.h>
  19. #include <LPlaceHolder.h>
  20. #include <UMemoryMgr.h>
  21. #include <UWindows.h>
  22. #include <String_Utils.h>
  23. #include <LTextEdit.h>
  24.  
  25. #include <Sound.h>
  26.  
  27. CTextDoc    *theDoc;
  28. extern    unsigned long     ourAvailRam;
  29.  
  30. const ResIDT    WIND_TextDoc        = 500;
  31. const ResIDT    prto_TextDoc        = 201;
  32.  
  33. #define kVictorySound 1000
  34. //    const OSType    Creator_DemoDoc        = 'PPd0';
  35. const ResIDT    STRx_Untitled        = 300;
  36.  
  37. Boolean                hasSmallScreen;
  38. CardWindow            *gMainWindow;
  39.  
  40.  
  41. // ---------------------------------------------------------------------------
  42. //        Ñ CTextDoc(LCommander*, FSSpec*)
  43. // ---------------------------------------------------------------------------
  44. //    Construct a TextDoc associated with the specified file
  45. //
  46. //    If inFileSpec is nil, then create an empty, untitled document
  47.  
  48.  
  49. CTextDoc::CTextDoc(
  50.     LCommander    *inSuper,
  51.     FSSpec        *inFileSpec)
  52.         : LSingleDoc(inSuper)
  53. {
  54.     CardStruct        theCardStruct;
  55.     
  56. //    short    i;
  57.     
  58.     if (qd.screenBits.bounds.right - qd.screenBits.bounds.left < 640)
  59.         hasSmallScreen = true;
  60.     else
  61.         hasSmallScreen = false;
  62.     
  63.     theDeck = new CardDeck;
  64.     
  65.     currentDeckWell = currentFreeCell = currentStack = 0;
  66.     
  67.     cardRgn = NewRgn();
  68.     theDoc = this;
  69.     
  70.     theCardStruct.card = kNoCard;
  71.     
  72.     firstCard = savedFromCard = savedToCard = nil;
  73. //    secondCard = nil;
  74.     
  75. //    firstCard.card = secondCard.card = kNoCard;
  76.     
  77.  
  78.                                     // Create window for our document
  79.     if (hasSmallScreen)
  80.         mWindow = CardWindow::CreateWindow(WIND_TextDoc +1, this);
  81.     else
  82.         mWindow = CardWindow::CreateWindow(WIND_TextDoc, this);
  83.         
  84.     gMainWindow = (CardWindow *)mWindow;
  85.     
  86.     
  87.                                     // Specify that the text view should
  88.                                     // be the Target when the Window
  89.                                     // is activated
  90. //    mTextView = (CDirtyText*) mWindow->FindPaneByID('Text');
  91. //    mWindow->SetLatentSub(mTextView);
  92.     
  93.     if (inFileSpec == nil) {
  94. //        NameNewDoc();                // Set name of untitled window
  95.         
  96.     } else {
  97.         OpenFile(*inFileSpec);        // Display contents of file in window
  98.     }
  99.     
  100. }
  101.  
  102. CTextDoc::~CTextDoc()            //    destructor
  103. {
  104.     delete theDeck;
  105.     theDeck = nil;
  106.     
  107.     theDoc = nil;
  108. }
  109.  
  110. // ---------------------------------------------------------------------------
  111. //        Ñ OpenFile
  112. // ---------------------------------------------------------------------------
  113. //    Open a new document for the specified File
  114.  
  115. void
  116. CTextDoc::OpenFile(
  117.     FSSpec    &inFileSpec)
  118. {
  119.         // Create a new File object, read the entire File contents,
  120.         // put the contents into the text view, and set the Window
  121.         // title to the name of the File.
  122.         
  123.     Try_ {
  124.         mFile = new LFile(inFileSpec);
  125.         mFile->OpenDataFork(fsRdWrPerm);
  126.         Handle    textH = mFile->ReadDataFork();
  127. //        mTextView->SetTextHandle(textH);
  128.         ::DisposeHandle(textH);
  129.         
  130.         mWindow->SetDescriptor(inFileSpec.name);
  131.         mIsSpecified = true;
  132.     }
  133.     
  134.     Catch_(inErr) {
  135.         delete this;
  136.         Throw_(inErr);
  137.     
  138.     } EndCatch_
  139. }
  140.  
  141.  
  142. // ---------------------------------------------------------------------------
  143. //        Ñ IsModified
  144. // ---------------------------------------------------------------------------
  145. //    Return whether the Document is has changed since the last save
  146.  
  147. Boolean
  148. CTextDoc::IsModified()
  149. {
  150.         // Document has changed if the text view is dirty
  151. //    mIsModified = mTextView->IsDirty();
  152.     return mIsModified;
  153. }
  154.  
  155.  
  156. // ---------------------------------------------------------------------------
  157. //        Ñ DoAESave
  158. // ---------------------------------------------------------------------------
  159. //    Save Document in the specified file with the specified file type
  160. //
  161. //    If file type is fileType_Default, use the normal file type for
  162. //    this document
  163.  
  164. void
  165. CTextDoc::DoAESave(
  166.     FSSpec    &inFileSpec,
  167.     OSType    inFileType)
  168. {
  169.     delete mFile;                        // Kill existing file
  170.     
  171.     mFile = new LFile(inFileSpec);        // Make new file object
  172.     
  173.     OSType    fileType = GameFileType;            // Find proper file type
  174.     if (inFileType != fileType_Default)
  175.     {
  176.         fileType = inFileType;
  177.     }
  178.                                         // Make new file on disk
  179.     mFile->CreateNewDataFile(OurCreator, GameFileType, 0);
  180.     mFile->OpenDataFork(fsRdWrPerm);
  181.     DoSave();                            // Write out data
  182.                                         // Change window name
  183.     mWindow->SetDescriptor(inFileSpec.name);
  184. }
  185.  
  186.  
  187. // ---------------------------------------------------------------------------
  188. //        Ñ DoSave
  189. // ---------------------------------------------------------------------------
  190. //    Save the entire Document to its associated File (which must already exist)
  191.  
  192. void
  193. CTextDoc::DoSave()
  194. {
  195.                                         // Get text and write to file
  196. //    Handle    textH = mTextView->GetTextHandle();
  197. //    StHandleLocker    theLock(textH);
  198. //    mFile->WriteDataFork(*textH, GetHandleSize(textH));
  199.     
  200. //    mTextView->SetDirty(false);            // Saving makes doc un-dirty
  201. }
  202.  
  203.  
  204. // ---------------------------------------------------------------------------
  205. //        Ñ DoRevert
  206. // ---------------------------------------------------------------------------
  207. //    Revert the Document to the last saved version on disk
  208.  
  209. void
  210. CTextDoc::DoRevert()
  211. {
  212. //    Handle    textH = mFile->ReadDataFork();
  213. //    mTextView->SetTextHandle(textH);
  214. //    ::DisposeHandle(textH);
  215. //    mTextView->Refresh();
  216. }
  217.  
  218.  
  219. // ---------------------------------------------------------------------------
  220. //        Ñ DoPrint
  221. // ---------------------------------------------------------------------------
  222. //    Print the contents of the Document
  223.  
  224. void
  225. CTextDoc::DoPrint()
  226. {
  227.     LPrintout        *thePrintout = LPrintout::CreatePrintout(prto_TextDoc);
  228.     LPlaceHolder    *textPlace = (LPlaceHolder*)
  229.                                     thePrintout->FindPaneByID('TBox');
  230. //    textPlace->InstallOccupant(mTextView, atNone);
  231.     
  232.     thePrintout->DoPrintJob();
  233.     delete thePrintout;
  234. }
  235.  
  236. void    CTextDoc::CheckVictory(void)
  237. {
  238.     short            i;
  239.     SndListHandle     mySndHandle;    // handle to an 'snd ' resource
  240.     SndChannelPtr    mySndChan;        // pointer to a sound channel
  241.     OSErr            myErr;
  242.  
  243.  
  244.     
  245.     for (i= 0; i < 4; i++)
  246.     {
  247.         if (theDeckWells[i]->itsCard->card != 13)        // it's not a king
  248.             return;
  249.     }
  250.     
  251.     
  252.     mySndChan = nil;     // Initialize channel pointer for error checking
  253.  
  254.     // Read in 'snd' resource from resource file
  255.     mySndHandle = (SndListHandle) GetResource ('snd ', kVictorySound);
  256.  
  257.     // check for a nil handle
  258.     if ( mySndHandle != nil )
  259.     {
  260.         myErr = SndPlay (mySndChan, mySndHandle, true);
  261.     }
  262. }
  263.  
  264. void    CTextDoc::StartNewGame(Boolean isFirstTime)
  265. {
  266.     short    i;
  267. //    GrafPtr    thePort = mWindow->GetMacPort();
  268. //    SetPort(thePort);
  269.  
  270.         theDeck->ZapOldDeck();
  271.         theDeck->GenerateNewDeck(false);
  272.         theDoc->currentDeckWell = theDoc->currentFreeCell = theDoc->currentStack = 0;
  273.  
  274.     for (i= 0; i < 4; i++)
  275.     {
  276.         theDeckWells[i]->itsCard = nil;
  277. //        theDeckWells[i]->Draw(nil);
  278.         
  279.         theFreeCells[i]->itsCard = nil;
  280. //        theFreeCells[i]->Draw(nil);
  281.  
  282.         theStacks[i]->itsCard = nil;
  283.         theStacks[i+4]->itsCard = nil;
  284.     }
  285.     
  286.     mWindow->Refresh();
  287.     mWindow->UpdatePort();
  288.  
  289.     for (i=0; i < 8; i++)
  290.     {
  291.         theStacks[i]->InitCards();
  292.  
  293.         if (!isFirstTime)
  294.             theStacks[i]->Draw(0L);
  295.     }
  296. }
  297.